package com.serivce;

import com.dao.*;
import com.mysql.cj.util.StringUtils;
import com.pojo.*;
import com.util.MyBatisHelp;
import com.util.Util;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class UserManagerSerivce {
    /**
     * 用户登录业务
     * @param account
     * @param password
     * @return
     */
    public Users userLogin(String account, String password){
        SqlSession session = MyBatisHelp.getSession();
        UsersDao usersDao = session.getMapper(UsersDao.class);

        Users users = usersDao.queryByAccount(account);
        //检查用户是否存在，且密码正确
        if(users!=null&&users.getPassword().equals(password)){
            return users;
        }
        return null;
    }

    /**
     * 查询所有用户业务
     * @return
     */
    public List<Users> queryAllUser(){
        SqlSession session = MyBatisHelp.getSession();
        UsersDao usersDao = session.getMapper(UsersDao.class);
        List<Users> users = usersDao.queryAllUser();

        return users;
    }

    /**
     * 添加学生用户业务
     * @return 影响行数
     */
    public int addUser(Users users){
        SqlSession session = MyBatisHelp.getSession();
        UsersDao usersDao = session.getMapper(UsersDao.class);

        int result = usersDao.addUser(users);
        if (result==1){
            session.commit();
            return result;
        }else {
            session.rollback();
            return 0;
        }
    }

    /**
     * 通过试题id列表,查询题库
     * @return 试题列表
     */
    public List<Subject> queryById(int singlenumber,int decidenumber){
        SqlSession session = MyBatisHelp.getSession();
        SubjectDao subjectDao = session.getMapper(SubjectDao.class);
        //单选题获取
        List<Integer> singleList = new ArrayList<>();
        List<Subject> subjects1 = subjectDao.queryByStype(1);
        for (Subject subject : subjects1) {
            int sid = subject.getSid();
            singleList.add(sid);
        }
        Collections.shuffle(singleList);
        HashSet hs = new HashSet();
        Random r = new Random();
        while (hs.size() < singlenumber){
            hs.add(r.nextInt(singleList.size())+1);
        }
        //判断题获取
        List<Integer> decideList = new ArrayList<>();
        List<Subject> subjects2 = subjectDao.queryByStype(2);
        for (Subject subject : subjects2) {
            int sid = subject.getSid();
            decideList.add(sid);
        }
        Collections.shuffle(decideList);
        while (hs.size() < singlenumber+decidenumber){
            hs.add(r.nextInt(decideList.size())+singleList.size()+1);
        }

        List<Integer> scontent = new ArrayList<>();
        for (Object i : hs) {
            scontent.add((Integer) i);
        }

        List<Subject> subjects = subjectDao.queryById(scontent);

        //获取问题id
        List<String> problemList = new ArrayList<>();
        for (Subject problem : subjects) {
            problemList.add(Integer.toString(problem.getSid()));
        }
        return subjects;
    }

    /**
     * 查询所有课程
     * @return 返回课程列表
     */
    public List<Course> queryAllCourse(){
        SqlSession session = MyBatisHelp.getSession();
        CourseDao courseDao = session.getMapper(CourseDao.class);

        List<Course> courses = courseDao.queryAllCourse();
        return courses;
    }

    /**
     * 查询所有班级
     * @return 返回班级列表
     */
    public List<Pjclass> queryAllClass(){
        SqlSession session = MyBatisHelp.getSession();
        PjclassDao pjclassDao = session.getMapper(PjclassDao.class);

        List<Pjclass> pjclasses = pjclassDao.queryAllClass();
        return pjclasses;
    }

    /**
     * 添加考试
     * @param exam
     * @return 影响行数
     */
    public int addExam(Exam exam){
        SqlSession session = MyBatisHelp.getSession();
        ExamDao examDao = session.getMapper(ExamDao.class);

        int result = examDao.addExam(exam);
        if (result!=0){
            session.commit();
            addPaper(exam);
            return result;
        }else {
            session.rollback();
            return 0;
        }
    }

    /**
     * id查询考试
     * @param id
     * @return
     */
    public Exam queryById(String id){
        SqlSession session = MyBatisHelp.getSession();
        ExamDao examDao = session.getMapper(ExamDao.class);

        Exam result = examDao.queryById(id);
//        System.out.println(exam);
        return result;
    }
    /**
     * 添加试卷
     * @param exam
     */
    public void addPaper(Exam exam){
        SqlSession session = MyBatisHelp.getSession();
        PaperDao paperDao = session.getMapper(PaperDao.class);
//        ExamDao examDao = session.getMapper(ExamDao.class);

//        List<Paper> paperList = paperDao.queryByAll();
        Paper paper = new Paper();
//        List<Exam> examList = examDao.queryByAll();
//        for (Exam exam : examList) {
//            for (Paper paper1 : paperList) {
//                if (paper1 != null) {
//                    if (exam.getEid() == paper1.getEid()) {
//                        System.out.println(exam.getEid());
//                    }
//                }
//                break;
//            }
            paper.setEid(exam.getEid());
            paper.setCno(exam.getCno());

            List<Subject> subjects = queryById(exam.getSinglenumber(), exam.getDecidenumber());
            //获取问题id
            List<String> problemList = new ArrayList<>();
            for (Subject problem : subjects) {
                problemList.add(Integer.toString(problem.getSid()));
            }

            paper.setScontent(String.join(",", problemList));
        int result = paperDao.addPaper(paper);
        if (result != 0) {
            session.commit();
        }else {
            session.rollback();
        }

    }

    /**
     * 查询所有试卷
     * @return 试卷了吧
     */
    public List<Paper> queryAllExam(){
        SqlSession session = MyBatisHelp.getSession();
        PaperDao paperDao = session.getMapper(PaperDao.class);

        List<Paper> paperList = paperDao.queryByAll();
        return paperList;
    }

    /**
     * id查询试卷scontent
     * @return 题目列表
     */
    public List<Subject> queryByPid(String pid){
        SqlSession session = MyBatisHelp.getSession();
        PaperDao paperDao = session.getMapper(PaperDao.class);

        Paper paper = paperDao.queryById(pid);
        String scontent = paper.getScontent();
        List<String> subjectIds = Arrays.asList(scontent.split(","));

        List<Integer> list = subjectIds.stream().map(Integer::parseInt).collect(Collectors.toList());

        SubjectDao subjectDao = session.getMapper(SubjectDao.class);
        List<Subject> subjectList = subjectDao.queryById(list);
        if (subjectList.size()!=0) {
            return subjectList;
        }
        return null;
    }
    /**
     * 删除试卷
     * @return 影响行数
     */
    public int deletePaper(String id){
        SqlSession session = MyBatisHelp.getSession();
        PaperDao paperDao = session.getMapper(PaperDao.class);

        int result = paperDao.deletePaper(Integer.parseInt(id));
        if (result!=0) {
            session.commit();
            return result;
        }else {
            session.rollback();
            return 0;
        }
    }

    /**
     * 添加学生成绩
     * @param studentExam
     * @return 影响行数
     */
    public int addScore(StudentExam studentExam){
        SqlSession session = MyBatisHelp.getSession();
        StudentExamDao studentExamDao = session.getMapper(StudentExamDao.class);

        int result = studentExamDao.addScore(studentExam);
        if (result!=0) {
            session.commit();
            return result;
        }else{
            session.rollback();
            return 0;
        }
//        return 0;
    }

    /**
     * 用户id查询成绩
     * @param userid
     * @return 成绩列表
     */
    public List<StudentExam> queryByUserid(String userid){
        SqlSession session = MyBatisHelp.getSession();
        StudentExamDao studentExamDao = session.getMapper(StudentExamDao.class);

        List<StudentExam> result = studentExamDao.queryByUserid(userid);
//        System.out.println(result);
        return result;
    }
    @Test
    public void testMain(){
        List<StudentExam> result = queryByUserid("8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strdate = "";
        for (StudentExam res : result) {
            Date date1 = new Date(String.valueOf(res.getTjtime()));
            strdate = sdf.format(date1);
            try {
                res.setTjtime(sdf.parse(strdate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println(result);
        //学生成绩表
//        queryByUserid("8");
//        queryByPid("2");
        //查询试卷
//        SqlSession session = MyBatisHelp.getSession();
//        PaperDao paperDao = session.getMapper(PaperDao.class);
//
//        List<Paper> paperList = paperDao.queryByAll();
//        System.out.println(paperList);
        //用户表
//        SqlSession session = MyBatisHelp.getSession();
//        UsersDao usersDao = session.getMapper(UsersDao.class);

//        Users users = usersDao.queryByAccount("admin");
//        List<Users> users = usersDao.queryAllUser();
//        System.out.println(users);
        //考试表
//        SqlSession session = MyBatisHelp.getSession();
//        SubjectDao subjectDao = session.getMapper(SubjectDao.class);
//
//        int num = 5;
//        List<Integer> singleList = new ArrayList<>();
//        List<Subject> subjects1 = subjectDao.queryByStype(1);
//        for (Subject subject : subjects1) {
//            int sid = subject.getSid();
//            singleList.add(sid);
//        }
//        Collections.shuffle(singleList);
//        HashSet hs = new HashSet();
//        Random r = new Random();
//        while (hs.size() < num){
//            hs.add(r.nextInt(singleList.size())+1);
//        }
//
//
//        List<Integer> decideList = new ArrayList<>();
//        List<Subject> subjects2 = subjectDao.queryByStype(2);
//        for (Subject subject : subjects2) {
//            int sid = subject.getSid();
//            decideList.add(sid);
//        }
//        Collections.shuffle(decideList);
//        while (hs.size() < num+3){
//            hs.add(r.nextInt(decideList.size())+singleList.size()+1);
//        }
//
//        List<Integer> scontent = new ArrayList<>();
//        for (Object i : hs) {
//            scontent.add((Integer) i);
//        }

//        List<Subject> subjects = subjectDao.queryById(scontent);
//        System.out.println("--------------");
//        //获取问题id
//        List<String> problemList = new ArrayList<>();
//        for (Subject problem : subjects) {
//            problemList.add(Integer.toString(problem.getSid()));
//        }
//        System.out.println(subjects);
//        System.out.println(hs);
//        System.out.println(problemList);
        //试卷表
//        SqlSession session = MyBatisHelp.getSession();
//        PaperDao paperDao = session.getMapper(PaperDao.class);
//        ExamDao examDao = session.getMapper(ExamDao.class);
//
//        Exam exam = examDao.queryById("4");
//        System.out.println(exam);
//        Paper paper = new Paper();
//        List<Exam> examList = examDao.queryByAll();
//        for (Exam exam : examList) {
//            paper.setEid(exam.getEid());
//            paper.setCno(exam.getCno());
//
//            List<Subject> subjects = queryById(exam.getSinglenumber(), exam.getDecidenumber());
//            //获取问题id
//            List<String> problemList = new ArrayList<>();
//            for (Subject problem : subjects) {
//                problemList.add(Integer.toString(problem.getSid()));
//            }
//
//            paper.setScontent(String.join(",", problemList));
//        }
//        addPaper();
//        System.out.println(paper);
        //课程表
//        SqlSession session = MyBatisHelp.getSession();
//        CourseDao courseDao = session.getMapper(CourseDao.class);
//        List<Course> courses = courseDao.queryAllCourse();
//        for (Course cour : courses) {
//            System.out.println(cour.getCname());
//        }
        //班级表
//        SqlSession session = MyBatisHelp.getSession();
//        PjclassDao pjclassDao = session.getMapper(PjclassDao.class);
//
//        List<Pjclass> pjclasses = pjclassDao.queryAllClass();
//        System.out.println(pjclasses);
    }
}
